package com.artPreter.service.impl;

import com.artPreter.mapper.ArtworkDepartRelativeMapper;
import com.artPreter.pojo.ArtworkDepartRelative;
import com.artPreter.pojo.ContentTag;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.artPreter.mapper.ContentArtworkRelativeMapper;
import com.artPreter.pojo.Artwork;
import com.artPreter.mapper.ArtworkMapper;
import com.artPreter.pojo.ContentArtworkRelative;
import com.artPreter.service.IArtworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 艺术品表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-14
 */
@Service
public class ArtworkServiceImpl extends ServiceImpl<ArtworkMapper, Artwork> implements IArtworkService {

    @Autowired
    private ContentArtworkRelativeMapper contentArtworkRelativeMapper;
    @Autowired
    private ArtworkDepartRelativeMapper artworkDepartRelativeMapper;





    @Override
    public void add(Artwork artwork) {
        artwork.setStatus(1);
        String uuid = UUID.randomUUID().toString();
        artwork.setArtworkId(uuid);
        this.baseMapper.insert(artwork);
        this.addArtworkDepartRelative(artwork,artwork.getDepartIds());
    }

    /**
     * 添加艺术品与机构的关联关系
     * @param artwork
     * @param departIds
     */
    private void addArtworkDepartRelative(Artwork artwork, String departIds) {
        String[] strings = departIds.split("，");
        for (String string : strings) {
            ArtworkDepartRelative artworkDepartRelative = new ArtworkDepartRelative();
            artworkDepartRelative.setArtworkId(artwork.getArtworkId());
            artworkDepartRelative.setDepartId(string);
            if(string!=null&& !string.isEmpty()){
                artworkDepartRelativeMapper.insert(artworkDepartRelative);
            }
        }
    }

    /**
     * 删除艺术品与机构的关联关系
     * @param artwork
     */
    private void deleteArtworkDepartRelative(Artwork artwork) {
        LambdaQueryWrapper<ArtworkDepartRelative> artworkDepartRelativeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        artworkDepartRelativeLambdaQueryWrapper.eq(ArtworkDepartRelative::getArtworkId,artwork.getArtworkId());
        artworkDepartRelativeMapper.delete(artworkDepartRelativeLambdaQueryWrapper);
    }

    @Override
    public void edit(Artwork artwork) {
        this.baseMapper.updateById(artwork);
        this.deleteArtworkDepartRelative(artwork);
        this.addArtworkDepartRelative(artwork,artwork.getDepartIds());
    }


    @Override
    public void delete(Artwork artwork) {
        LambdaQueryWrapper<Artwork> artworkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        artworkLambdaQueryWrapper.eq(Artwork::getId,artwork.getId());
        List<Artwork> artworks = this.baseMapper.selectList(artworkLambdaQueryWrapper);
        if(artworks.size()>0){
            Artwork deleteItem = artworks.get(0);
            // 删除艺术品相关联的内容
            if(deleteItem.getArtworkId()!=null&&!"".equals(deleteItem.getArtworkId())){
                LambdaQueryWrapper<ContentArtworkRelative> contentArtworkRelativeLambdaQueryWrapper = new LambdaQueryWrapper<>();
                contentArtworkRelativeLambdaQueryWrapper.eq(ContentArtworkRelative::getArtworkId,deleteItem.getArtworkId());
                contentArtworkRelativeMapper.delete(contentArtworkRelativeLambdaQueryWrapper);
            }
            this.baseMapper.deleteById(deleteItem);
        }
    }

    @Override
    public IPage<Artwork> getPage(Integer currentPage, Integer size, Artwork artwork) {
        // 创建分页对象
        IPage<Artwork> page = new Page<>(currentPage, size);
        // 构建查询条件
        LambdaQueryWrapper<Artwork> queryWrapper = new LambdaQueryWrapper<>();
        // 排序
        queryWrapper.orderByDesc(Artwork::getCreateTime);
        queryWrapper.eq(Artwork::getStatus,1);
        // 艺术品名字
        if(artwork.getName()!=null&&!"".equals(artwork.getName())){
            queryWrapper.like(Artwork::getName,artwork.getName());
        }
        // 艺术家名字
        if(artwork.getArtistName()!= null &&!"".equals(artwork.getArtistName())) {
            queryWrapper.like(Artwork::getArtistName, artwork.getArtistName());
        }
        // 艺术家id
        if(artwork.getArtistId()!= null&&!"".equals(artwork.getArtistId())){
            queryWrapper.eq(Artwork::getArtistId,artwork.getArtistId());

        }


        // 调用分页查询方法
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void addToRelative(Artwork artwork) {

        ContentArtworkRelative contentArtworkRelative = new ContentArtworkRelative();
        if(artwork.getArtworkId()!=null&&!"".equals(artwork.getArtworkId())){
            contentArtworkRelative.setArtworkId(artwork.getArtworkId());
        }else {
            return;
        }
        if(artwork.getContentId()!=null&&!"".equals(artwork.getContentId())){
            contentArtworkRelative.setContentId(artwork.getContentId());
        }else {
            return;
        }
        contentArtworkRelative.setTags(artwork.getTag());
        LambdaQueryWrapper<ContentArtworkRelative> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContentArtworkRelative::getContentId,artwork.getContentId());
        queryWrapper.eq(ContentArtworkRelative::getArtworkId,artwork.getArtworkId());
        if(contentArtworkRelativeMapper.selectCount(queryWrapper)>0){
            return;
        }else{
            contentArtworkRelativeMapper.insert(contentArtworkRelative);
        }
    }

    @Override
    public void addToOnlineRelative(Artwork artwork) {

    }



    @Override
    public void removeArtworkRelative(Artwork artwork) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(artwork.getArtworkId()!=null&&!"".equals(artwork.getArtworkId())){
            queryWrapper.eq("artwork_id",artwork.getArtworkId());
        }else {
            return;
        }
        if(artwork.getContentId()!=null&&!"".equals(artwork.getContentId())){
            queryWrapper.eq("content_id",artwork.getContentId());
        }else {
            return;
        }
       contentArtworkRelativeMapper.delete(queryWrapper);

    }

    @Override
    public IPage<Artwork> getPageNoRelative(Integer currentPage, Integer size, Artwork artwork) {
        LambdaQueryWrapper<Artwork> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Artwork::getStatus,1);
        queryWrapper.orderByDesc(Artwork::getId);
        if(artwork.getType()!=null && (artwork.getType().equals(1)||artwork.getType().equals(3))){
            queryWrapper.eq(Artwork::getType,1);
        }else if(artwork.getType()!=null &&artwork.getType().equals(0)){
            queryWrapper.eq(Artwork::getType,0);
        }
        String departId = artwork.getDepartId();
        String artistId = artwork.getArtistId();
        // 内容id
        String contentId = artwork.getContentId();

        if(departId!=null&&!"".equals(departId)){
            System.out.println(departId+"机构id");
            queryWrapper.notExists("SELECT 1 FROM jv_content_artwork_relative WHERE jv_content_artwork_relative.content_id = '"+contentId+"' and jv_content_artwork_relative.artwork_id = jv_artwork.artwork_id");
            if(artwork.getType()!=null && (artwork.getType().equals(1))){
                queryWrapper.exists("SELECT 1 FROM jv_artist_relative WHERE jv_artist_relative.artist_id = jv_artwork.artist_id and jv_artist_relative.depart_id = '"+departId+"' and jv_artist_relative.status = 1");
            }
        }else if(artistId!=null&&!"".equals(artistId)){
            queryWrapper.notExists("SELECT 1 FROM jv_content_artwork_relative WHERE jv_content_artwork_relative.content_id = '"+contentId+"' and jv_content_artwork_relative.artwork_id = jv_artwork.artwork_id");
            queryWrapper.eq(Artwork::getArtistId,artistId);

        }else {
            return null;
        }


        IPage<Artwork> artworkIPage = new Page<>(currentPage,size);
        // 艺术品名字模糊搜索
        if(artwork.getName()!=null&&!"".equals(artwork.getName())){
            queryWrapper.like(Artwork::getName,artwork.getName());
        }
        // 艺术家名字模糊搜索
        if (artwork.getArtistName()!= null &&!"".equals(artwork.getArtistName())) {
            queryWrapper.like(Artwork::getArtistName, artwork.getArtistName());
        }
        // 艺术品模编号糊搜索
        if(artwork.getArtworkNum()!=null&&!"".equals(artwork.getArtworkNum())){
            queryWrapper.like(Artwork::getArtworkNum,artwork.getArtworkNum());
        }


        artworkIPage = this.baseMapper.selectPage(artworkIPage, queryWrapper);
        return artworkIPage;
    }


    @Override
    public IPage<Artwork> getPageNoRelativeOnline(Integer currentPage, Integer size, Artwork artwork) {
        QueryWrapper<Artwork> queryWrapper = new QueryWrapper<>();

        String departId = artwork.getDepartId();
        queryWrapper.eq("b.depart_id",departId);


        IPage<Artwork> artworkIPage = new Page<>(currentPage,size);
        // 艺术品名字模糊搜索
        if(artwork.getName()!=null&&!"".equals(artwork.getName())){
            queryWrapper.like("b.name",artwork.getName());
        }
        // 艺术家名字模糊搜索
        if(artwork.getArtistName()!=null&&!"".equals(artwork.getArtistName())){
            queryWrapper.like("b.artist_name",artwork.getArtistName());

        }

        queryWrapper.eq("b.depart_id",departId);
        queryWrapper.eq("s.type",1);
        queryWrapper.notExists("SELECT 1 FROM jv_artwork_depart_relative c WHERE c.depart_id = '"+departId+"' and c.artwork_id = s.artwork_id");


        artworkIPage = this.baseMapper.getPageNoRelativeOnline(artworkIPage, queryWrapper);
        return artworkIPage;
    }

    @Override
    public IPage<Artwork> getPageRelativeOnline(Integer currentPage, Integer size, Artwork artwork) {
        QueryWrapper<Artwork> queryWrapper = new QueryWrapper<>();

        String departId = artwork.getDepartId();
        queryWrapper.eq("s.depart_id",departId);


        IPage<Artwork> artworkIPage = new Page<>(currentPage,size);
        // 艺术品名字模糊搜索
        if(artwork.getName()!=null&&!"".equals(artwork.getName())){
            queryWrapper.like("b.name",artwork.getName());
        }
        // 艺术家名字模糊搜索
        if(artwork.getArtistName()!=null&&!"".equals(artwork.getArtistName())){
            queryWrapper.like("b.artist_name",artwork.getArtistName());

        }


        artworkIPage = this.baseMapper.getPageRelativeOnline(artworkIPage, queryWrapper);
        return artworkIPage;
    }

    @Override
    public void deleteByArtistId(String artistId) {
        if(artistId!=null){
            LambdaQueryWrapper<Artwork> artworkLambdaQueryWrapper = new LambdaQueryWrapper<>();
            artworkLambdaQueryWrapper.eq(Artwork::getArtistId,artistId);
            List<Artwork> artworkList = this.baseMapper.selectList(artworkLambdaQueryWrapper);
            for (int i = 0; i < artworkList.size(); i++) {
                this.delete(artworkList.get(i));
            }
        }
    }


    @Override
    public IPage<Artwork> getPageRelative(Integer currentPage, Integer size, Artwork artwork) {
        LambdaQueryWrapper<Artwork> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Artwork::getStatus,1);
        queryWrapper.orderByDesc(Artwork::getId);
        String departId = artwork.getDepartId();
        // 内容id
        String contentId = artwork.getContentId();
        String artistId = artwork.getArtistId();
        if(departId!=null&&!"".equals(departId)){
            System.out.println("机构已关联");
            queryWrapper.exists("SELECT 1 FROM jv_content_artwork_relative WHERE jv_content_artwork_relative.content_id = '"+contentId+"' and jv_content_artwork_relative.artwork_id = jv_artwork.artwork_id");
            queryWrapper.exists("SELECT 1 FROM jv_artist_relative WHERE jv_artist_relative.artist_id = jv_artwork.artist_id and jv_artist_relative.depart_id = '"+departId+"' and jv_artist_relative.status = 1");
        }else if(artistId!=null&&!"".equals(artistId)){
            queryWrapper.exists("SELECT 1 FROM jv_content_artwork_relative WHERE jv_content_artwork_relative.content_id = '"+contentId+"' and jv_content_artwork_relative.artwork_id = jv_artwork.artwork_id");
            queryWrapper.eq(Artwork::getArtistId,artistId);
        }else {
            return null;
        }

        IPage<Artwork> artworkIPage = new Page<>(currentPage,size);
        // 艺术品名字模糊搜索
        if(artwork.getName()!=null&&!"".equals(artwork.getName())){
            queryWrapper.like(Artwork::getName,artwork.getName());
        }
        // 艺术家名字模糊搜索
        if (artwork.getArtistName()!= null &&!"".equals(artwork.getArtistName())) {
            queryWrapper.like(Artwork::getArtistName, artwork.getArtistName());
        }
        // 艺术品模编号糊搜索
        if(artwork.getArtworkNum()!=null&&!"".equals(artwork.getArtworkNum())){
            queryWrapper.like(Artwork::getArtworkNum,artwork.getArtworkNum());
        }
        artworkIPage = this.baseMapper.selectPage(artworkIPage, queryWrapper);
        ContentTag contentTag = new ContentTag();
        contentTag.setContentId(contentId);
        for (int i = 0; i < artworkIPage.getRecords().size(); i++) {
            LambdaQueryWrapper<ContentArtworkRelative> contentArtworkRelativeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            contentArtworkRelativeLambdaQueryWrapper.eq(ContentArtworkRelative::getArtworkId,artworkIPage.getRecords().get(i).getArtworkId());
            contentArtworkRelativeLambdaQueryWrapper.eq(ContentArtworkRelative::getContentId,contentId);
            List<ContentArtworkRelative> contentArtworkRelatives = contentArtworkRelativeMapper.selectList(contentArtworkRelativeLambdaQueryWrapper);
            if(contentArtworkRelatives.size()>0){
                artworkIPage.getRecords().get(i).setTag(contentArtworkRelatives.get(0).getTags());
            }

        }

        return artworkIPage;
    }


}
